<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/7/8
 * Time: 12:30
 */
class GamebaseAction extends BaseAction
{
    function __construct()
    {
        parent::__construct();
    }

    function GetGameTableName($act, $t)
    {
        $tablegame = "";
        $tablegame_auto = "";
        $tablegame_auto_tz = "";
        $tablegame_kg_users_tz = "";
        $tablegame_users_tz = "";
        $tableret = "";
        switch ($act) {
            case "0"://gamefast28
                $tablegame = "gamefast28";
                $tablegame_auto = "gamefast28_auto";
                $tablegame_auto_tz = "gamefast28_auto_tz";
                $tablegame_kg_users_tz = "gamefast28_kg_users_tz";
                $tablegame_users_tz = "gamefast28_users_tz";
                break;
            case "1"://gamefast16
                $tablegame = "gamefast16";
                $tablegame_auto = "gamefast16_auto";
                $tablegame_auto_tz = "gamefast16_auto_tz";
                $tablegame_kg_users_tz = "gamefast16_kg_users_tz";
                $tablegame_users_tz = "gamefast16_users_tz";
                break;
            case "2"://gamefast11
                $tablegame = "gamefast11";
                $tablegame_auto = "gamefast11_auto";
                $tablegame_auto_tz = "gamefast11_auto_tz";
                $tablegame_kg_users_tz = "gamefast11_kg_users_tz";
                $tablegame_users_tz = "gamefast11_users_tz";
                break;
            case "3"://game28
                $tablegame = "game28";
                $tablegame_auto = "game28_auto";
                $tablegame_auto_tz = "game28_auto_tz";
                $tablegame_kg_users_tz = "game28_kg_users_tz";
                $tablegame_users_tz = "game28_users_tz";
                break;
            case "4":
                $tablegame = "gameself28";
                $tablegame_auto = "gameself28_auto";
                $tablegame_auto_tz = "gameself28_auto_tz";
                $tablegame_kg_users_tz = "gameself28_kg_users_tz";
                $tablegame_users_tz = "gameself28_users_tz";
                break;
            case "5":
                $tablegame = "gamebj16";
                $tablegame_auto = "gamebj16_auto";
                $tablegame_auto_tz = "gamebj16_auto_tz";
                $tablegame_kg_users_tz = "gamebj16_kg_users_tz";
                $tablegame_users_tz = "gamebj16_users_tz";
                break;
            case "6":
                $tablegame = "gamepk10";
                $tablegame_auto = "gamepk10_auto";
                $tablegame_auto_tz = "gamepk10_auto_tz";
                $tablegame_kg_users_tz = "gamepk10_kg_users_tz";
                $tablegame_users_tz = "gamepk10_users_tz";
                break;
            case "7":
                $tablegame = "gamegj10";
                $tablegame_auto = "gamegj10_auto";
                $tablegame_auto_tz = "gamegj10_auto_tz";
                $tablegame_kg_users_tz = "gamegj10_kg_users_tz";
                $tablegame_users_tz = "gamegj10_users_tz";
                break;
            case "8":
                $tablegame = "gamecan28";
                $tablegame_auto = "gamecan28_auto";
                $tablegame_auto_tz = "gamecan28_auto_tz";
                $tablegame_kg_users_tz = "gamecan28_kg_users_tz";
                $tablegame_users_tz = "gamecan28_users_tz";
                break;
            case "9":
                $tablegame = "gamecan16";
                $tablegame_auto = "gamecan16_auto";
                $tablegame_auto_tz = "gamecan16_auto_tz";
                $tablegame_kg_users_tz = "gamecan16_kg_users_tz";
                $tablegame_users_tz = "gamecan16_users_tz";
                break;
            case "10":
                $tablegame = "gamecan11";
                $tablegame_auto = "gamecan11_auto";
                $tablegame_auto_tz = "gamecan11_auto_tz";
                $tablegame_kg_users_tz = "gamecan11_kg_users_tz";
                $tablegame_users_tz = "gamecan11_users_tz";
                break;
            case "11":
                $tablegame = "game36";
                $tablegame_auto = "game36_auto";
                $tablegame_auto_tz = "game36_auto_tz";
                $tablegame_kg_users_tz = "game36_kg_users_tz";
                $tablegame_users_tz = "game36_users_tz";
                break;
            case "12":
                $tablegame = "gamebj36";
                $tablegame_auto = "gamebj36_auto";
                $tablegame_auto_tz = "gamebj36_auto_tz";
                $tablegame_kg_users_tz = "gamebj36_kg_users_tz";
                $tablegame_users_tz = "gamebj36_users_tz";
                break;
            case "13":
                $tablegame = "gamecan36";
                $tablegame_auto = "gamecan36_auto";
                $tablegame_auto_tz = "gamecan36_auto_tz";
                $tablegame_kg_users_tz = "gamecan36_kg_users_tz";
                $tablegame_users_tz = "gamecan36_users_tz";
                break;
            case "14":
                $tablegame = "gamepk22";
                $tablegame_auto = "gamepk22_auto";
                $tablegame_auto_tz = "gamepk22_auto_tz";
                $tablegame_kg_users_tz = "gamepk22_kg_users_tz";
                $tablegame_users_tz = "gamepk22_users_tz";
                break;
            case "15":
                $tablegame = "gamefast10";
                $tablegame_auto = "gamefast10_auto";
                $tablegame_auto_tz = "gamefast10_auto_tz";
                $tablegame_kg_users_tz = "gamefast10_kg_users_tz";
                $tablegame_users_tz = "gamefast10_users_tz";
                break;
            case "16":
                $tablegame = "gamepklh";
                $tablegame_auto = "gamepklh_auto";
                $tablegame_auto_tz = "gamepklh_auto_tz";
                $tablegame_kg_users_tz = "gamepklh_kg_users_tz";
                $tablegame_users_tz = "gamepklh_users_tz";
                break;
            case "17":
                $tablegame = "gamepkgyj";
                $tablegame_auto = "gamepkgyj_auto";
                $tablegame_auto_tz = "gamepkgyj_auto_tz";
                $tablegame_kg_users_tz = "gamepkgyj_kg_users_tz";
                $tablegame_users_tz = "gamepkgyj_users_tz";
                break;
            case "18":
                $tablegame = "gamehg28";
                $tablegame_auto = "gamehg28_auto";
                $tablegame_auto_tz = "gamehg28_auto_tz";
                $tablegame_kg_users_tz = "gamehg28_kg_users_tz";
                $tablegame_users_tz = "gamehg28_users_tz";
                break;
            case "19":
                $tablegame = "gamehg16";
                $tablegame_auto = "gamehg16_auto";
                $tablegame_auto_tz = "gamehg16_auto_tz";
                $tablegame_kg_users_tz = "gamehg16_kg_users_tz";
                $tablegame_users_tz = "gamehg16_users_tz";
                break;
            case "20":
                $tablegame = "gamehg11";
                $tablegame_auto = "gamehg11_auto";
                $tablegame_auto_tz = "gamehg11_auto_tz";
                $tablegame_kg_users_tz = "gamehg11_kg_users_tz";
                $tablegame_users_tz = "gamehg11_users_tz";
                break;
            case "21":
                $tablegame = "gamehg36";
                $tablegame_auto = "gamehg36_auto";
                $tablegame_auto_tz = "gamehg36_auto_tz";
                $tablegame_kg_users_tz = "gamehg36_kg_users_tz";
                $tablegame_users_tz = "gamehg36_users_tz";
                break;
            default:
                break;

        }
        switch ($t) {
            case "game":
                $tableret = $tablegame;
                break;
            case "auto":
                $tableret = $tablegame_auto;
                break;
            case "auto_tz":
                $tableret = $tablegame_auto_tz;
                break;
            case "kg_users_tz":
                $tableret = $tablegame_kg_users_tz;
                break;
            case "users_tz":
                $tableret = $tablegame_users_tz;
                break;
            default:
                break;
        }
        return $tableret;
    }
    function GetGameConfig($act){
        $json=ApplicationRegistry::getCache('game_config');
        if($json){
            $result= json_decode($json,true);
            foreach ($result as $k=>$v){
                if($v['game_type']==$act)return $v;
            }
            return $result[$act];
        }else {
            $sql = 'select game_type,game_name,game_table_prefix from game_config';
            $list = db::get_all($sql,'assoc');
            $result=array();
            foreach ($list as $k=>$v){
                if($v['game_type']==$act)$result=$v;
            }
            $json = json_encode($list, JSON_UNESCAPED_UNICODE);
            //ApplicationRegistry::setConf(
            ApplicationRegistry::setCache('game_config', $json);
            
            return $result;
        }
        return array();
    }

    function GetHeadContent($act, $sid, &$aret)
    {
        $tablegame = $this->GetGameTableName($act, "game");
        $tablegametz = $this->GetGameTableName($act, "users_tz");
        $SecondSub = -90;
        if (in_array($act,[0,1,2,15])) $SecondSub = -10;//急速
        //取当前待开奖
        $sql = "SELECT id,kgtime,now() as nowtime FROM {$tablegame} WHERE kj = 0 AND kgtime > DATE_ADD(NOW(),interval {$SecondSub} second) ORDER BY id LIMIT 1";
        $result = db::get_one($sql, 'assoc');
        $aret['preno'] = $result["id"];
        $aret['prekgtime'] = $this->DateDiff($result["kgtime"], $result["nowtime"], "s");
        //取游戏配置
        $sql = "select game_kj_delay,game_tz_close from game_config where game_type='{$act}'";
        $result = db::get_one($sql, 'assoc');
        $aret['game_kj_delay'] = $result['game_kj_delay'];
        $aret['game_tz_close'] = $result['game_tz_close'];
        return true;
    }
    function GetNewNo($act){
        $SecondSub=$this->delay_time($act);
        $tablegame = $this->GetGameTableName($act, "game");
        $sql = "SELECT id,kgtime,now() as nowtime FROM {$tablegame} WHERE kj = 0 AND kgtime > DATE_ADD(NOW(),interval {$SecondSub} second) ORDER BY id LIMIT 1";
        $result = db::get_one($sql, 'assoc');
        return $result['id'];
    }
    function delay_time($act){
        $SecondSub = -90;
        if (in_array($act,[0,1,2,15])) $SecondSub = -10;//急速
        return $SecondSub;
    }
    function GetKjTzTime($act){
        $sql = "select game_kj_delay,game_tz_close from game_config where game_type='{$act}'";
        $result = db::get_one($sql, 'assoc');
        $result=array('game_kj_delay'=>$result['game_kj_delay'],'game_tz_close'=>$result['game_tz_close']);
        return $result;
    }
    function GetCurrNo($act,$no){
        $tablegame = $this->GetGameTableName($act, "game");
        $aret=array();
        //取当前待开奖
        $sql = "SELECT id,kj,kgtime,now() as nowtime FROM {$tablegame} WHERE id=$no ORDER BY id LIMIT 1";
        $result = db::get_one($sql, 'assoc');
        $aret['preno'] = $result["id"];
        $aret['prekgtime'] = $this->DateDiff($result["kgtime"], $result["nowtime"], "s");
        $aret['kj']=$result['kj'];
        //取游戏配置
        $sql = "select game_kj_delay,game_tz_close from game_config where game_type='{$act}'";
        $result = db::get_one($sql, 'assoc');
        $aret['kjSec'] = $aret['prekgtime'] + $result['game_kj_delay'];
        $aret['StopSec'] = $aret['prekgtime'] - $result['game_tz_close'];
        return $aret;
    }
    function DateDiff($date1, $date2, $unit = ""){
        switch ($unit) {
            case 's':
                $dividend = 1;
                break;
            case 'i':
                $dividend = 60;
                break;
            case 'h':
                $dividend = 3600;
                break;
            case 'd':
                $dividend = 86400;
                break;
            default:
                $dividend = 86400;
        }
        $time1 = strtotime($date1);
        $time2 = strtotime($date2);
        if ($time1 && $time2)
            return (float)($time1 - $time2) / $dividend;
        return false;
    }

    //取离第一个开奖数字步长
    function GetFromBeginNumStep($GameType)
    {
        $step = 0;
        if ($GameType == "14")
            $step = 6;
        else if ($GameType == "1" || $GameType == "5" || $GameType == "9" || $GameType == "17" || $GameType == "19")
            $step = 3;
        else if ($GameType == "2" || $GameType == "10" || $GameType == "20")
            $step = 2;
        else if ($GameType == "6" || $GameType == "7" || $GameType == "11" || $GameType == "12" || $GameType == "13" || $GameType == "15" || $GameType == "16" || $GameType == "21")
            $step = 1;

        return $step;
    }
    //取赔率类型
    function GetGameOddsType($act)
    {
        $reward_num_type = "game28";
        if($act == "1" || $act == "5" || $act == "9" || $act == "19")
            $reward_num_type = "game16";
        else if($act == "2" || $act == "10" || $act == "20")
            $reward_num_type = "game11";
        else if($act == "6" || $act == "7" || $act == "15")
            $reward_num_type = "game10";
        else if($act == "11" || $act == "12" || $act == "13" || $act == "21")
            $reward_num_type = "game36";
        else if($act == "14")
            $reward_num_type = "game22";
        else if($act == "16")
            $reward_num_type = "gamelh";
        else if($act == "17")
            $reward_num_type = "gamegyj";

        return  $reward_num_type;
    }

    //获取游戏的赔率
    function get_config($id)
    {
        $sql='select game_press_min as press_min,game_press_max as press_max,game_std_press as pressNum from game_config where game_type='.$id;
        $row=db::get_one($sql);
        $row->gameType = count(explode(',', $row->pressNum));
        return $row;
    }
 
}